<template>
  <div class="exam">

    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="formInline.key" placeholder="请输入关键字" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>

        <el-button type="primary" @click="addDialog = true">新增招聘会</el-button>
        <el-dialog title="新增岗位" :visible.sync="addDialog">
          <el-form v-model="addForm">

            <el-form-item label="公司名称" :label-width="formLabelWidth">
              <el-input v-model="addForm.Name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="公司地址" :label-width="formLabelWidth">
              <el-input v-model="addForm.Address" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" :label-width="formLabelWidth">
              <el-input v-model="addForm.ContactInfo" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="职位名称" :label-width="formLabelWidth">
              <el-input v-model="addForm.PositionName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="薪水" :label-width="formLabelWidth">
              <el-input v-model="addForm.Salary" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="岗位要求" :label-width="formLabelWidth">
              <el-input v-model="addForm.Responsibilities" autocomplete="off"></el-input>
            </el-form-item>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="addDialog = false">取 消</el-button>
            <el-button type="primary" @click="Add">确 定</el-button>
          </div>
        </el-dialog>

        <el-button type="primary" @click="getInterviewInfo">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="form"
      style="width: 100%"
      border>
      <el-table-column
        label="单位名称">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.Name }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="单位地址">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.Address }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="联系方式">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.ContactInfo }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="职位名称">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.PositionName }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="薪水">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.Salary }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="岗位要求">
        <template v-slot="scope">
          <span style="margin-left: 10px">{{ scope.row.Responsibilities }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="编辑">

        <template v-slot="scope">
          <!--        编辑对话框-->
          <el-button type="primary" icon="el-icon-edit" @click="openEditDialog(scope.$index)"></el-button>
          <el-dialog title="编辑信息" :visible.sync="dialogVisibles[scope.$index]">

            <el-form v-model="editForm">
              <el-form-item label="地址" :label-width="formLabelWidth">
                <el-input v-model="editForm.Address" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="联系方式" :label-width="formLabelWidth">
                <el-input v-model="editForm.ContactInfo" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="岗位要求" :label-width="formLabelWidth">
                <el-input v-model="editForm.Responsibilities" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogVisibles[scope.$index] = false">取 消</el-button>
              <el-button type="primary" @click="Edit(scope.$index,scope.row)">确 定</el-button>
            </div>
          </el-dialog>

          <!--        删除对话框-->
          <el-button type="primary" icon="el-icon-delete" @click="openDeleteDialog(scope.$index)"></el-button>
          <el-dialog
            title="提示"
            :visible.sync="DeleteDialogVisibles[scope.$index]"
            width="30%">
            <span>确定删除信息？</span>
            <span slot="footer" class="dialog-footer">
            <el-button @click="DeleteDialogVisibles[scope.$index] = false">取 消</el-button>
            <el-button type="primary" @click="Delete(scope.$index,scope.row)">确 定</el-button>
          </span>
          </el-dialog>
        </template>

      </el-table-column>

    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.current"
      :page-sizes="[5, 10]"
      :page-size="pagination.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      class="page"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  name: "interviewManage",
  data() {
    return {
      form: [], //后端获取招聘会信息
      editForm:{
      Name: "",
      Address: "",
      ContactInfo: "",
      PositionName: "",
      Salary: null,
      Responsibilities: ""
    },//编辑框修改的数据
      addForm:{
        Address: "",
        PositionName: "",
        Salary: null,
        ContactInfo: "",
        Responsibilities: "",
        Name: ""
      },//新增招聘会信息
      pagination: { //分页后的考试信息
        current: 1, //当前页
        total: null, //记录条数
        size: 5//每页条数
      },
      formInline: {
        key: '',
        type: ''
      },
      addDialog:false,
      formLabelWidth:'90px',
      dialogVisibles:{},
      DeleteDialogVisibles:{}
    }
  },
  mounted() {
    this.getInterviewInfo()
  },
  methods: {
    getInterviewInfo() { //分页查询所有招聘信息
      this.$axios({
        url:'/api/interview/interviewList',
        method:'get',
        params:{
          page:this.pagination.current,
          size:this.pagination.size,
        },
        headers:{
          "Authorization":this.$cookies.get("token")
        }
      }).then(res =>{
        console.log(res.data)
        this.pagination=res.data.data
        this.form = res.data.data.records
        console.log(this.form)
      })
    },
    //改变当前记录条数
    handleSizeChange(val) {
      this.pagination.size = val
      this.getInterviewInfo()
    },
    //改变当前页码，重新发送请求
    handleCurrentChange(val) {
      this.pagination.current = val
      this.getInterviewInfo()
    },
    //查询
    onSubmit(){
      this.$axios({
        url:'/api/interview/search',
        method:'get',
        params: {
          page:this.pagination.current,
          size:this.pagination.size,
          key:this.formInline.key
        },
        headers:{
          'Authorization':this.$cookies.get('token')
        }
      }).then(res =>{
        this.pagination=res.data.data
        this.form = res.data.data.records
      })
    },
    //编辑
    Edit(index,row){
      this.dialogVisibles[this.form.indexOf(row)] = false
      this.editForm.Name = this.form[index].Name
      this.editForm.PositionName = this.form[index].PositionName
      this.editForm.Salary = this.form[index].Salary
      this.$axios({
        url:'/api/interview/editInterview',
        method:'post',
        data:this.editForm,
        headers:{
          'Authorization':this.$cookies.get('token')
        }
      }).then(res =>{
        if(res.data.code===0){
          this.$message.success("更新成功")
          this.getInterviewInfo()
        }else {
          this.$message.error("更新失败")
        }
      })
    },
    // 删除
    Delete(index,row){
      this.DeleteDialogVisibles[this.form.indexOf(row)] = false
      this.$axios({
        url:'/api/interview/deleteInterview',
        method:'delete',
        params:{
          name:this.form[index].Name,
          position:this.form[index].PositionName,
          salary:this.form[index].Salary
        },
        headers:{
          'Authorization':this.$cookies.get('token')
        }
      }).then(res =>{
        // console.log(res.data)
        if(res.data.code === 0){
          this.$message.success("删除成功")
          this.getInterviewInfo()
        }else {
          this.$message.error("删除失败")
        }
      })
    },
    //新增
    Add(){
      this.addDialog = false
      this.$axios({
        url:'/api/interview/addInterview',
        method:'post',
        data:this.addForm,
        headers:{
          'Authorization':this.$cookies.get('token')
        }
      }).then(res =>{
        if(res.data.code === 0){
          this.$message.success("新增成功")
          this.getInterviewInfo()
        }else {
          this.$message.error("添加失败")
        }
      })
    },

    openEditDialog(index){
      this.$set(this.dialogVisibles,index,true)
    },
    openDeleteDialog(index){
      this.$set(this.DeleteDialogVisibles,index,true)
    }
  },
}
</script>

<style lang="less" scoped>
.exam {
  padding: 0px 40px;
.page {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.edit{
  margin-left: 20px;
}
}
</style>
